package com.housekeeping.housekeeping.cleanerInformation.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author hua
 * @version JDK1.8
 * @date 2021/1/23 9:48
 *      图片上传后将需要响应给前端的数据封装为json格式并响应给前端
 */
public class ResponseImagesUtils {
    public static final String CONTENT_TYPE_JSON = "application/json;charset=utf-8";

    public static void responseImages(HttpServletResponse response, ResponseImages images) throws IOException {
        /* 允许跨域的主机地址 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        response.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        response.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        response.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        response.setHeader("Access-Control-Allow-Credentials", "true");
        //设置响应类型为枚举类型
        response.setContentType(ResponseUtils.CONTENT_TYPE_JSON);
        //将images 转为json格式数据响应给前端
        response.getWriter().print(JSONObject.toJSONString(images));
    }
}
